<template>
  <div>
    <h1>拼团活动列表</h1>
    <div>
      <el-form :inline="true" :model="listFrom" class="demo-form-inline">
        <el-form-item label="活动名称">
          <el-input
            v-model="listFrom.keyWord"
            placeholder="请输入活动名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list()">查询</el-button>
          <el-button type="danger" @click="onaddGroupActive()"
            >添加活动</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="activeId" label="活动Id" />
      <el-table-column prop="activeName" label="活动名称" />
      <el-table-column prop="groupNum" label="组团数量" />
      <el-table-column prop="beginTime" label="开始时间" />
      <el-table-column prop="endTime" label="结束时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="info" @click="OnDeleteActive(scope.row)"
            >删除</el-button
          >
          <el-button type="succrss" @click="ShowUpdateActive(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="listFrom.pageNum"
        v-model:page-size="listFrom.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        size="small"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog
        v-model="dialogFormVisible"
        title="添加拼团活动页面"
        width="500"
      >
        <el-form
          :model="form"
          :rules="rules"
          ref="ruleFormRef"
          label-width="auto"
        >
          <el-form-item
            label="活动名称"
            :label-width="formLabelWidth"
            prop="activeName"
          >
            <el-input v-model="form.activeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="活动数量"
            :label-width="formLabelWidth"
            prop="groupNum"
          >
            <el-input-number
              min="1"
              v-model="form.groupNum"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item
            label="活动时间"
            :label-width="formLabelWidth"
            prop="getTimeList"
          >
            <el-date-picker
              v-model="form.getTimeList"
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm:ss"
              date-format="YYYY/MM/DD ddd"
              time-format="A hh:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
    <div>
      <el-dialog v-model="dialogTableVisible" title="修改活动页面" width="500">
        <el-form :model="form">
          <el-form-item label="活动名称" :label-width="formLabelWidth">
            <el-input v-model="form.activeName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="活动数量" :label-width="formLabelWidth">
            <el-input v-model="form.groupNum" autocomplete="off" />
          </el-form-item>
          <el-form-item label="活动开始时间" :label-width="formLabelWidth">
            <el-input v-model="form.beginTime" autocomplete="off" />
          </el-form-item>
          <el-form-item label="活动结束时间" :label-width="formLabelWidth">
            <el-input v-model="form.endTime" autocomplete="off" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="OnUpdateActive()">
              修改
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import {
  getGroupActiveList,
  addGroupActive,
  deleteActive,
  updateActive,
} from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
const tableData = ref([]);
const total = ref(0);
const dialogFormVisible = ref(false);
const dialogTableVisible = ref(false);
const formLabelWidth = "140px";
const listFrom = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
});
const form = ref({
  activeId: null,
  activeName: "",
  beginTime: "",
  endTime: "",
  groupNum: null,
  getTimeList: [],
});
const ruleFormRef = ref<FormInstance>();
const handleChange = (value: number | undefined) => {
  console.log(value);
};
//列表
const list = () => {
  getGroupActiveList(listFrom.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
//添加
const onaddGroupActive = () => {
  dialogFormVisible.value = true;
};
//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  listFrom.value.pageSize = val;
  list();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  listFrom.value.pageNum = val;
  list();
};
//校验
const rules = reactive<FormRules>({
  activeName: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
  groupNum: [{ required: true, message: "请输入数量", trigger: "blur" }],
  getTimeList: [
    { required: true, message: "请选择开始时间和结束时间", trigger: "blur" },
  ],
});
//添加
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");

      addGroupActive(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          list();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
//删除
const OnDeleteActive = (row: any) => {
  ElMessageBox.confirm(
    "一旦删除就无法恢复，请您三思啊，确定要删除吗?",
    "删除过度警告框",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "error",
    }
  )
    .then(() => {
      deleteActive(row.activeId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("用户删除成功!");
          list();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户已取消删除",
      });
    });
};
//修改
const ShowUpdateActive = (row: any) => {
  dialogTableVisible.value = true;
  form.value = row;
};
const OnUpdateActive = () => {
  updateActive(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("用户修改成功!");
      dialogTableVisible.value = false;
      list();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
onMounted(() => {
  list();
});
</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
